package com.tarena.lbs.business.dao.repository.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.mapper.StoreMapper;
import com.tarena.lbs.business.dao.repository.StoreESRepository;
import com.tarena.lbs.business.dao.repository.StoreRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.po.StorePO;
import com.tarena.lbs.business.pojo.po.StoreSearchPO;
import com.tarena.lbs.business.pojo.query.StoreQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class StoreRepositoryImpl implements StoreRepository {
    @Autowired
    StoreMapper storeMapper;
    @Autowired
    BusinessMapper businessMapper;
    @Autowired
    StoreESRepository storeESRepository;
    
    @Override
    public void save(StorePO storePO) {
        // 先保存到MySQL
        storeMapper.insert(storePO);
        
        // 再保存到Elasticsearch
        StoreSearchPO storeSearchPO = new StoreSearchPO();
        BeanUtils.copyProperties(storePO, storeSearchPO);
        
        // 设置地理位置字段
        if (storePO.getStoreLatitude() != null && storePO.getStoreLongitude() != null) {
            storeSearchPO.setLocation(storePO.getStoreLatitude() + "," + storePO.getStoreLongitude());
        }
        
        storeESRepository.save(storeSearchPO);
    }

    @Override
    public List<StorePO> pageList(StoreQuery query) {
        LambdaQueryWrapper<StorePO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query.getStoreName()), StorePO::getStoreName, query.getStoreName());
        wrapper.like(StringUtils.hasText(query.getStoreHeadPhone()), StorePO::getStoreHeadPhone, query.getStoreHeadPhone());
        //areaId businessId cityId
        wrapper.eq(query.getAreaId() != null, StorePO::getAreaId, query.getAreaId());
        wrapper.eq(query.getBusinessId() != null, StorePO::getBusinessId, query.getBusinessId());
        wrapper.eq(query.getCityId() != null, StorePO::getCityId, query.getCityId());
        //startingTime endTime
        wrapper.between(query.getStartingTime() != null && query.getEndTime() != null,
                StorePO::getUpdateTime, query.getStartingTime(), query.getEndTime());
        wrapper.orderByDesc(StorePO::getUpdateTime);
        //storeStatus
        wrapper.eq(query.getStoreStatus() != null, StorePO::getStoreStatus, query.getStoreStatus());



        return storeMapper.selectList(wrapper);
    }

    @Override
    public BusinessPO selectByBusinessId(Integer businessId) {
        return businessMapper.selectById(businessId);
    }

    @Override
    public StorePO selectByStoreId(Integer id) {
        return storeMapper.selectById(id);
    }

    @Override
    public void updateById(StorePO storePO) {
        // 先更新MySQL
        QueryWrapper<StorePO> storePOQueryWrapper = new QueryWrapper<>();
        storePOQueryWrapper.eq("id", storePO.getId());
        storeMapper.update(storePO, storePOQueryWrapper);
        
        // 再更新Elasticsearch
        StoreSearchPO storeSearchPO = new StoreSearchPO();
        BeanUtils.copyProperties(storePO, storeSearchPO);
        storeESRepository.save(storeSearchPO);
    }

    @Override
    public List<StorePO> getStoreByCity(Long areaId) {
        QueryWrapper<StorePO> storePOQueryWrapper = new QueryWrapper<>();
        storePOQueryWrapper.eq("area_id", areaId);
        return storeMapper.selectList(storePOQueryWrapper);
    }

    @Override
    public void deleteById(Integer id) {
        storeMapper.deleteById(id);
        storeESRepository.deleteById(id);
    }

    @Override
    public List<StorePO> pageList(Long areaId, String storeName) {
        LambdaQueryWrapper<StorePO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(areaId != null, StorePO::getAreaId, areaId);
        wrapper.like(StringUtils.hasText(storeName), StorePO::getStoreName, storeName);
        return storeMapper.selectList(wrapper);
    }
}